package com.hllxd.hism.star.basic.service.service;

import com.alibaba.fastjson.JSON;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.DataMapMapper;
import com.hllxd.hism.star.basic.service.model.metric.MetricSourceEntity;
import com.hllxd.hism.star.basic.service.model.metric.MetricSourceRelationEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@Service("dataMapService")
public class DataMapServiceImpl implements DataMapService {

    @Autowired
    private DataMapMapper dataMapMapper;


    @Override public MetricSourceEntity getMetricSource(Long id) {
        MetricSourceEntity entity = dataMapMapper.getMetricSource(id);
        if(entity==null) {
            return null;
        }
        // 获取表相关的上下游关系表
        entity.relationEntityList = dataMapMapper.listMetricSourceRelation(entity.topicTbl);
        return entity;
    }

    @Override public MetricSourceEntity getMetricSourceByTbl(String tbl) {
        MetricSourceEntity entity = dataMapMapper.getMetricSourceByTbl(tbl);
        if(entity==null) {
            return null;
        }
        // 获取表相关的上下游关系表
        entity.relationEntityList = dataMapMapper.listMetricSourceRelation(tbl);
        return entity;
    }

    private List<MetricSourceRelationEntity> buildRelations(String topicTbl, String sourceRelation) {
        List<MetricSourceRelationEntity> relationEntityList = new ArrayList<>();
        if(sourceRelation.isEmpty()) {
            return relationEntityList;
        }
        String[] sourceTblList = sourceRelation.split(",");
        for(String sourceTbl: sourceTblList) {
            MetricSourceRelationEntity relationEntity = new MetricSourceRelationEntity();
            relationEntity.topicTbl = topicTbl;
            relationEntity.topicTblSource = sourceTbl.trim();
            relationEntityList.add(relationEntity);
        }
        return relationEntityList;
    }
    @Override
    public Long setMetricSource(MetricSourceEntity entity) {
        log.info("set param: {}", JSON.toJSONString(entity));

        List<MetricSourceRelationEntity> relationList = buildRelations(entity.topicTbl, entity.sourceRelation);
        // 清理历史关系
        Boolean ret = dataMapMapper.deleteMetricSourceRelation(entity.topicTbl);
        // 写入当前关系
        Long res = dataMapMapper.insertMetricSourceRelation(relationList);

        if(entity.id==null || entity.id==0) {
            log.info("will insert entity.");
            return dataMapMapper.insertMetricSource(entity);
        } else {
            log.info("will update entity.");
            return dataMapMapper.updateMetricSource(entity);
        }
    }
    @Override
    public List<MetricSourceEntity> listMetricTopic() {
        return dataMapMapper.listMetricTopic();
    }

    @Override
    public List<MetricSourceEntity> listMetricSource(String topic) {
        return dataMapMapper.listMetricSource(topic);
    }

    @Override
    public List<MetricSourceEntity> searchMetricSource(String search) {
        if(search.isEmpty()) {
            return new ArrayList<>();
        }
        return dataMapMapper.searchMetricSource('%' + search + '%');
    }

    @Override
    public Boolean deleteMetricSource(String ids) {
        return dataMapMapper.deleteMetricSource(ids);
    }

//    @Override
//    public MetricSourceEntity syncDpMeta(String topicTbl) {
//        return dataMapMapper.syncDpMeta(topicTbl);
//    }
}
